package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ReflectDemo7 {
    public static void main(String[] args) throws Exception{
//        File dir = new File(ReflectDemo7.class.getClassLoader().getResource(".").toURI());
        File dir = new File((ReflectDemo7.class.getResource(".").toURI()));
//        System.out.println(dir.getAbsolutePath());
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub :subs){
            String fileName = sub.getName();
            System.out.println(fileName);
            String packageName = ReflectDemo7.class.getPackage().getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            System.out.println("类名："+className);

            Class cls = Class.forName(packageName+"."+className);
//            System.out.println("加载的类为:"+cls.getName());
            Object obj = cls.newInstance();

            Method[] methods = cls.getDeclaredMethods();
            for (Method method:methods){
                if (method.getParameterCount()==0
                &&
                    method.getModifiers()== Modifier.PUBLIC){
                    System.out.println("自动调用"+className+"的方法");
                    method.invoke(obj);
                }
            }


        }
    }
}
